﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using SqlMiM.Common.View;
using SqlMiM.Common.Options;
using SqlMiM.Common.Framework.Configuration;
using SqlMiM.Common.Interfaces;

namespace SqlMiM.Common.Themes
{
    public sealed class ThemeOptionsPage : IOptionsPage
    {
        public const String NAME = "ThemeOptionsPage";
        // Default value fot theme
        public const string ACTIVE_THEME_DEFAULT_VALUE = "Dark Theme";
        public const string ACTIVE_THEME_PROPERTY = "ActiveTheme";

        // View
        private ThemeOptions optionControl;
        
        // ViewModel
        private ThemeOptionsView optionViewModel;

        // Configuration Object (contains all confis options)
        private ConfigurationObject configurationObject;

        
        public ThemeOptionsPage()
        {

        }

        public void Cancel()
        {
            if (this.optionViewModel != null)
            {
                this.optionViewModel = null;
                this.optionControl = null;
            }
        }

        public void Commit()
        {
            if (this.optionControl == null)
                return;

            var newThemeSelected = this.optionViewModel.SelectedTheme;

            if (newThemeSelected != ThemesServices.Current.ActiveTheme)
            {
                // Add new property to my ConfigurationObject (which will be saved after)
                this.configurationObject.SetProperty(ThemeOptionsPage.ACTIVE_THEME_PROPERTY, newThemeSelected.Name);

                // Update Theme
                ThemesServices.Current.UpdateActiveTheme(newThemeSelected.Name);
            }

            this.optionViewModel = null;
            this.optionControl = null;
        }

        public void Load(ConfigurationObject value)
        {
            this.configurationObject = value;
        }

        // Properties
        public object Content
        {
            get
            {
                if (this.optionControl == null)
                {
                    this.optionControl = new ThemeOptions();
                    this.optionViewModel = new ThemeOptionsView();
                    this.optionControl.DataContext = this.optionViewModel;

                }

                return this.optionControl;
            }
        }

        public string Name
        {
            get
            {
                return "Themes";
            }
        }

        public string Title
        {
            get
            {
                return "Themes";
            }
        }
    }


}
